using System.ComponentModel;

namespace Bindings
{
    public class TrackedProperty<T> : INotifyPropertyChanged, INotifyPropertyChanging
    {
        private T value;
        private readonly string PropertyName;
        public event PropertyChangedEventHandler PropertyChanged;
        public event PropertyChangingEventHandler PropertyChanging;


        public TrackedProperty(string propertyName, PropertyChangedEventHandler propertyChanged,
                               PropertyChangingEventHandler propertyChanging)
        {
            PropertyName = propertyName;
            PropertyChanged = propertyChanged;
            PropertyChanging = propertyChanging;
        }


        public void Set(T val)
        {
            if (!ReferenceEquals(value, null) && value.Equals(val)) return;

            PropertyChanging(this, new PropertyChangingEventArgs(PropertyName));
            value = val;
            PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }


        public T Get()
        {
            return value;
        }
    }
}